package sk.vasicek.drinkmanager.activity;

import java.math.BigInteger;
import java.util.ArrayList;

import sk.vasicek.drinkmanager.R;
import sk.vasicek.drinkmanager.domain.Drink;
import sk.vasicek.drinkmanager.service.BackgroundAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

public class DrinkManagerListActivity extends Activity {
	/** Called when the activity is first created. */
	// Beer
	private Button beerButton;
	private TextView beerCountText;
	private TextView beerCountLabel;
	private int beerCount = 0;
	// private SeekBar beerBar;
	private EditText beerPrice;
	private TextView beerPriceCurrencyLabel;
	// Shot
	private Button shotButton;
	private TextView shotCountText;
	private TextView shotCountLabel;
	private int shotCount = 0;
	// private SeekBar shotBar;
	private EditText shotPrice;
	private TextView shotPriceCurrencyLabel;
	// Wine
	private Button wineButton;
	private TextView wineCountText;
	private TextView wineCountLabel;
	private int wineCount = 0;
	// private SeekBar wineBar;
	private EditText winePrice;
	private TextView winePriceCurrencyLabel;
	// // Soft drinks
	// // Carbonated
	// private Button carbButton;
	// private TextView carbCountText;
	// private TextView carbCountLabel;
	// private int carbCount = 0;
	// private SeekBar carbBar;
	// private EditText carbPrice;
	// private TextView carbPriceCurrencyLabel;
	// // Coffee
	// private Button coffeeButton;
	// private TextView coffeeCountText;
	// private TextView coffeeCountLabel;
	// private int coffeeCount = 0;
	// private SeekBar coffeeBar;
	// private EditText coffeePrice;
	// private TextView coffeePriceCurrencyLabel;
	// // Energy drink
	// private Button energyButton;
	// private TextView energyCountText;
	// private TextView energyCountLabel;
	// private int energyCount = 0;
	// private SeekBar energyBar;
	// private EditText energyPrice;
	// private TextView energyPriceCurrencyLabel;
	// // Mineral water
	// private Button mineralButton;
	// private TextView mineralCountText;
	// private TextView mineralCountLabel;
	// private int mineralCount = 0;
	// private SeekBar mineralBar;
	// private EditText mineralPrice;
	// private TextView mineralPriceCurrencyLabel;
	// Stats
	private Button checkStatsButton;
	private TextView finalPriceText;
	private TextView alcoholConcentrationText;
	private RatingBar drunkRating;
	protected ImageButton settingsButton;
	protected ImageButton mapButton;
	protected ImageButton placesButton;
	protected ImageButton drinksButton;

	enum DrinkUI {
		beer(R.string.beerLabel, "beerButton", "beerPrice", "beerLabel", "beerCount"), 
		wine(R.string.wineLabel, "wineButton", "winePrice", "wineLabel", "wineCount"), 
		shot(R.string.shotLabel, "shotButton", "shotPrice", "shotLabel", "shotCount"), ;

		Integer labelText;
		Long buttonId;
		Long priceId;
		Long labelId;
		Long countId;

		private DrinkUI(Integer buttonText, String buttonId, String priceId, String labelId, String countId) {
			this.labelText = buttonText;
			this.buttonId = toHexString(buttonId);
			this.priceId = toHexString(priceId);
			this.labelId = toHexString(labelId);
			this.countId = toHexString(countId);
		}

		public static Long toHexString(String str) {
			return new BigInteger(1, str.getBytes()).longValue();
		}

		public static String fromHexString(Integer raw) {
			String hex = Integer.toHexString(raw).toUpperCase();
			StringBuilder str = new StringBuilder();
			for (int i = 0; i < hex.length(); i += 2) {
				str.append((char) Integer.parseInt(hex.substring(i, i + 2), 16));
			}
			return str.toString();
		}

		private Integer getLabelText() {
			return labelText;
		}

		private void setLabelText(Integer labelText) {
			this.labelText = labelText;
		}

		private Long getButtonId() {
			return buttonId;
		}

		private void setButtonId(Long buttonId) {
			this.buttonId = buttonId;
		}

		private Long getPriceId() {
			return priceId;
		}

		private void setPriceId(Long priceId) {
			this.priceId = priceId;
		}

		private Long getLabelId() {
			return labelId;
		}

		private void setLabelId(Long labelId) {
			this.labelId = labelId;
		}

		private Long getCountId() {
			return countId;
		}

		private void setCountId(Long countId) {
			this.countId = countId;
		}

	}

	private static String TAG = "DrinkManagerListActivity";

	ArrayList<Drink> drinks = null;
	SharedPreferences prefs;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.drinkmanagerlist);
		Log.d(TAG, "Started drink manager");
		// drinks storage
		this.drinks = new ArrayList<Drink>();

		// old implementation
		// beer controllers
		this.beerButton = (Button) this.findViewById(R.id.beerButton);
		this.beerPrice = (EditText) this.findViewById(R.id.beerPrice);
		this.beerCountLabel = (TextView) this.findViewById(R.id.beerLabel);
		this.beerCountText = (TextView) this.findViewById(R.id.beerCount);
		this.beerPriceCurrencyLabel = (TextView) this.findViewById(R.id.beerPriceCurrency);
		// this.beerBar = (SeekBar) this.findViewById(R.id.beerBar);
		// wine controllers
		this.wineButton = (Button) this.findViewById(R.id.wineButton);
		this.winePrice = (EditText) this.findViewById(R.id.winePrice);
		this.wineCountLabel = (TextView) this.findViewById(R.id.wineLabel);
		this.wineCountText = (TextView) this.findViewById(R.id.wineCount);
		this.winePriceCurrencyLabel = (TextView) this.findViewById(R.id.winePriceCurrency);
		// this.wineBar = (SeekBar) this.findViewById(R.id.wineBar);
		// shot controllers
		this.shotButton = (Button) this.findViewById(R.id.shotButton);
		this.shotPrice = (EditText) this.findViewById(R.id.shotPrice);
		this.shotCountLabel = (TextView) this.findViewById(R.id.shotLabel);
		this.shotCountText = (TextView) this.findViewById(R.id.shotCount);
		this.shotPriceCurrencyLabel = (TextView) this.findViewById(R.id.shotPriceCurrency);
		// this.shotBar = (SeekBar) this.findViewById(R.id.shotBar);

		LinearLayout list = (LinearLayout) this.findViewById(R.id.drinkControllList);
		LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		for (DrinkUI drinkUI : DrinkUI.values()) {
			Log.d(TAG, "Generating " + drinkUI.getLabelText() + " controll");
			View customView = linflater.inflate(R.layout.drink_controll, null);
			Button drinkButton = (Button) customView.findViewById(R.id.drinkButton);
			EditText drinkPrice = (EditText) customView.findViewById(R.id.drinkPrice);
			TextView drinkLabel = (TextView) customView.findViewById(R.id.drinkLabel);
			TextView drinkCount = (TextView) customView.findViewById(R.id.drinkCount);
			drinkButton.setId(drinkUI.getButtonId().intValue());
			drinkPrice.setId(drinkUI.getPriceId().intValue());
			drinkLabel.setId(drinkUI.getLabelId().intValue());
			drinkCount.setId(drinkUI.getCountId().intValue());
			drinkLabel.setText(getResources().getText(drinkUI.getLabelText()));
			drinkButton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Toast.makeText(DrinkManagerListActivity.this, v.getId() + "", Toast.LENGTH_SHORT).show();
				}
			});
			list.addView(customView);
		}

		// // Soft drinks
		// // Carbonated drink
		// this.carbButton = (Button)
		// this.findViewById(R.id.carbonatedDrinkButton);
		// this.carbPrice = (EditText)
		// this.findViewById(R.id.carbonatedDrinkPrice);
		// this.carbCountLabel = (TextView)
		// this.findViewById(R.id.carbonatedDrinkLabel);
		// this.carbCountText = (TextView)
		// this.findViewById(R.id.carbonatedDrinkCount);
		// this.carbPriceCurrencyLabel = (TextView)
		// this.findViewById(R.id.carbonatedDrinkPriceCurrency);
		// this.carbBar = (SeekBar) this.findViewById(R.id.carbonatedDrinkBar);
		// // Coffee
		// this.coffeeButton = (Button) this.findViewById(R.id.coffeeButton);
		// this.coffeePrice = (EditText) this.findViewById(R.id.coffeePrice);
		// this.coffeeCountLabel = (TextView)
		// this.findViewById(R.id.coffeeLabel);
		// this.coffeeCountText = (TextView)
		// this.findViewById(R.id.coffeeCount);
		// this.coffeePriceCurrencyLabel = (TextView)
		// this.findViewById(R.id.coffeePriceCurrency);
		// this.coffeeBar = (SeekBar) this.findViewById(R.id.coffeeBar);
		// // Energy drink
		// this.energyButton = (Button)
		// this.findViewById(R.id.energyDrinkButton);
		// this.energyPrice = (EditText)
		// this.findViewById(R.id.energyDrinkPrice);
		// this.energyCountLabel = (TextView)
		// this.findViewById(R.id.energyDrinkLabel);
		// this.energyCountText = (TextView)
		// this.findViewById(R.id.energyDrinkCount);
		// this.energyPriceCurrencyLabel = (TextView)
		// this.findViewById(R.id.energyDrinkPriceCurrency);
		// this.energyBar = (SeekBar) this.findViewById(R.id.energyDrinkBar);
		// // Mineral water
		// this.mineralButton = (Button)
		// this.findViewById(R.id.mineralWaterButton);
		// this.mineralPrice = (EditText)
		// this.findViewById(R.id.mineralWaterPrice);
		// this.mineralCountLabel = (TextView)
		// this.findViewById(R.id.mineralWaterLabel);
		// this.mineralCountText = (TextView)
		// this.findViewById(R.id.mineralWaterCount);
		// this.mineralPriceCurrencyLabel = (TextView)
		// this.findViewById(R.id.mineralWaterPriceCurrency);
		// this.mineralBar = (SeekBar) this.findViewById(R.id.mineralWaterBar);

		// stats controllers
		this.checkStatsButton = (Button) this.findViewById(R.id.checkStatsButton);
		this.drunkRating = (RatingBar) this.findViewById(R.id.drunkRating);
		this.finalPriceText = (TextView) this.findViewById(R.id.finalPriceText);
		this.alcoholConcentrationText = (TextView) this.findViewById(R.id.alcoholConcentration);

		String beerPriceText = "0.0";
		String winePriceText = "0.0";
		String shotPriceText = "0.0";

		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		BackgroundAdapter.updateBackgroundFromPrefs(prefs, this);
		// this.checkAndSetVisibility(prefs);
		/*
		 * boolean enableDefaultPrice = prefs.getBoolean("EnablePrice", false);
		 * if (enableDefaultPrice) { beerPriceText =
		 * prefs.getString("BeerPrice", "1.2"); winePriceText =
		 * prefs.getString("WinePrice", "1.0"); shotPriceText =
		 * prefs.getString("ShotPrice", "1.5");
		 * 
		 * DrinkManagerListActivity.this.shotPrice.setText(shotPriceText);
		 * DrinkManagerListActivity.this.beerPrice.setText(beerPriceText);
		 * DrinkManagerListActivity.this.winePrice.setText(winePriceText); }
		 * 
		 * this.beerButton.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { SharedPreferences prefs =
		 * PreferenceManager.getDefaultSharedPreferences(v.getContext()); Float
		 * beerAlcoholConcentration =
		 * Float.parseFloat(prefs.getString("BeerStrength",
		 * "0.04").replaceAll(",", ".")); Float beerVolume =
		 * Float.parseFloat(prefs.getString("BeerVolume", "0.5").replaceAll(",",
		 * "."));
		 * 
		 * DrinkManagerListActivity.this.drinks.add(new
		 * Drink(beerAlcoholConcentration, beerVolume, new Date()));
		 * 
		 * DrinkManagerListActivity.this.beerCount++;
		 * DrinkManagerListActivity.this
		 * .beerCountText.setText(String.valueOf(DrinkManagerListActivity
		 * .this.beerCount)); //
		 * DrinkManagerListActivity.this.beerBar.setProgress
		 * (DrinkManagerListActivity.this.beerCount); } });
		 * 
		 * // this.beerBar.setOnSeekBarChangeListener(new
		 * OnSeekBarChangeListener() // { // // @Override // public void
		 * onStopTrackingTouch(SeekBar seekBar) { // // TODO Auto-generated
		 * method stub // // } // // @Override // public void
		 * onStartTrackingTouch(SeekBar seekBar) { // // TODO Auto-generated
		 * method stub // // } // // @Override // public void
		 * onProgressChanged(SeekBar seekBar, int progress, boolean // fromUser)
		 * { // DrinkManagerListActivity.this.beerCount = //
		 * DrinkManagerListActivity.this.beerBar.getProgress(); //
		 * DrinkManagerListActivity
		 * .this.beerCountText.setText(String.valueOf(DrinkManagerListActivity
		 * .this.beerCount)); // // //
		 * DrinkCounterActivity.this.beerBar.setProgress
		 * (DrinkCounterActivity.this.beerCount); // // } // }); //
		 * this.shotBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
		 * // { // // @Override // public void onStopTrackingTouch(SeekBar
		 * seekBar) { // // TODO Auto-generated method stub // // } // //
		 * @Override // public void onStartTrackingTouch(SeekBar seekBar) { //
		 * // TODO Auto-generated method stub // // } // // @Override // public
		 * void onProgressChanged(SeekBar seekBar, int progress, boolean //
		 * fromUser) { // DrinkManagerListActivity.this.shotCount = //
		 * DrinkManagerListActivity.this.shotBar.getProgress(); //
		 * DrinkManagerListActivity
		 * .this.shotCountText.setText(String.valueOf(DrinkManagerListActivity
		 * .this.shotCount)); // // //
		 * DrinkCounterActivity.this.beerBar.setProgress
		 * (DrinkCounterActivity.this.beerCount); // // } // });
		 * this.shotButton.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { SharedPreferences prefs =
		 * PreferenceManager.getDefaultSharedPreferences(v.getContext()); Float
		 * shotAlcoholConcentration =
		 * Float.parseFloat(prefs.getString("ShotStrength",
		 * "0.39").replaceAll(",", ".")); Float shotVolume =
		 * Float.parseFloat(prefs.getString("ShotVolume",
		 * "0.05").replaceAll(",", "."));
		 * 
		 * DrinkManagerListActivity.this.drinks.add(new
		 * Drink(shotAlcoholConcentration, shotVolume, new Date()));
		 * 
		 * DrinkManagerListActivity.this.shotCount++;
		 * DrinkManagerListActivity.this
		 * .shotCountText.setText(String.valueOf(DrinkManagerListActivity
		 * .this.shotCount)); //
		 * DrinkManagerListActivity.this.shotBar.setProgress
		 * (DrinkManagerListActivity.this.shotCount); } }); //
		 * this.wineBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
		 * // { // // @Override // public void onStopTrackingTouch(SeekBar
		 * seekBar) { // // TODO Auto-generated method stub // // } // //
		 * @Override // public void onStartTrackingTouch(SeekBar seekBar) { //
		 * // TODO Auto-generated method stub // // } // // @Override // public
		 * void onProgressChanged(SeekBar seekBar, int progress, boolean //
		 * fromUser) { // DrinkManagerListActivity.this.wineCount = //
		 * DrinkManagerListActivity.this.wineBar.getProgress(); //
		 * DrinkManagerListActivity
		 * .this.wineCountText.setText(String.valueOf(DrinkManagerListActivity
		 * .this.wineCount)); // // //
		 * DrinkCounterActivity.this.beerBar.setProgress
		 * (DrinkCounterActivity.this.beerCount); // // } // });
		 * this.wineButton.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { SharedPreferences prefs =
		 * PreferenceManager.getDefaultSharedPreferences(v.getContext()); Float
		 * wineAlcoholConcentration =
		 * Float.parseFloat(prefs.getString("WineStrength",
		 * "0.16").replaceAll(",", ".")); Float wineVolume =
		 * Float.parseFloat(prefs.getString("WineVolume", "0.2").replaceAll(",",
		 * "."));
		 * 
		 * DrinkManagerListActivity.this.drinks.add(new
		 * Drink(wineAlcoholConcentration, wineVolume, new Date()));
		 * 
		 * DrinkManagerListActivity.this.wineCount++;
		 * DrinkManagerListActivity.this
		 * .wineCountText.setText(String.valueOf(DrinkManagerListActivity
		 * .this.wineCount)); //
		 * DrinkManagerListActivity.this.wineBar.setProgress
		 * (DrinkManagerListActivity.this.wineCount); } });
		 */
		this.checkStatsButton.setOnClickListener(new OnClickListener() {

			private float alcoholMassCoeficientBySex = new Float("0.68");
			private final float ethanolConcentration = new Float("0.8");
			private float bodyWeight = new Float("75");
			float beerAlcoholConcentration = new Float("0.04");
			float beerVolume = new Float("0.5");
			float wineAlcoholConcentration = new Float("0.16");
			float wineVolume = new Float("0.2");
			float shotAlcoholConcentration = new Float("0.39");
			float shotVolume = new Float("0.05");

			float alcoholConcentration = 0.0f;
			float drunkRating = this.alcoholConcentration * 100;

			@Override
			public void onClick(View v) {
				SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(v.getContext());
				boolean enableBeer = false;
				enableBeer = prefs.getBoolean("EnableBeer", true);
				boolean enableWine = false;
				enableWine = prefs.getBoolean("EnableWine", true);
				boolean enableShot = false;
				enableShot = prefs.getBoolean("EnableShot", true);
				if (enableBeer) {
					this.beerAlcoholConcentration = Float.parseFloat(prefs.getString("BeerStrength", "0.04").replaceAll(",", "."));
					this.beerVolume = Float.parseFloat(prefs.getString("BeerVolume", "0.5").replaceAll(",", "."));

				}
				Log.d(TAG, "beerAlcoholConcentration = " + this.beerAlcoholConcentration);
				Log.d(TAG, "beerVolume = " + this.beerVolume);

				if (enableWine) {
					this.wineAlcoholConcentration = Float.parseFloat(prefs.getString("WineStrength", "0.16").replaceAll(",", "."));
					this.wineVolume = Float.parseFloat(prefs.getString("WineVolume", "0.2").replaceAll(",", "."));

				}
				Log.d(TAG, "wineAlcoholConcentration = " + this.wineAlcoholConcentration);
				Log.d(TAG, "wineVolume = " + this.wineVolume);

				if (enableShot) {
					this.shotAlcoholConcentration = Float.parseFloat(prefs.getString("ShotStrength", "0.39").replaceAll(",", "."));
					this.shotVolume = Float.parseFloat(prefs.getString("ShotVolume", "0.05").replaceAll(",", "."));
				}
				Log.d(TAG, "shotAlcoholConcentration = " + this.shotAlcoholConcentration);
				Log.d(TAG, "shotVolume = " + this.shotVolume);

				int gender = Integer.parseInt(prefs.getString("Gender", "1"));
				switch (gender) {
				case 1:
					this.alcoholMassCoeficientBySex = 0.70f;
					break;
				case 2:
					this.alcoholMassCoeficientBySex = 0.60f;
					break;
				default:
					Log.d(TAG, "In alcoholMassCoeficientBySex loading had to take default value");
					this.alcoholMassCoeficientBySex = 0.70f;
					break;
				}
				Log.d(TAG, "alcoholMassCoeficientBySex = " + this.alcoholMassCoeficientBySex);

				this.bodyWeight = Float.parseFloat(prefs.getString("Weight", "75").replaceAll(",", "."));
				Log.d(TAG, "bodyWeight = " + this.bodyWeight);

				this.alcoholConcentration = (((DrinkManagerListActivity.this.beerCount * this.beerVolume * this.beerAlcoholConcentration * this.ethanolConcentration)
						+ (DrinkManagerListActivity.this.shotCount * this.shotVolume * this.shotAlcoholConcentration * this.ethanolConcentration) + (DrinkManagerListActivity.this.wineCount
						* this.wineVolume * this.wineAlcoholConcentration * this.ethanolConcentration)) / (this.bodyWeight * this.alcoholMassCoeficientBySex));
				this.drunkRating = this.alcoholConcentration * 1000f * 3f;
				Log.d(TAG, "blood alcohol concentration = " + this.alcoholConcentration);

				float beerFinalPrice = new Float("0.0");
				float shotFinalPrice = new Float("0.0");
				float wineFinalPrice = new Float("0.0");

				if (enableBeer) {
					if (DrinkManagerListActivity.this.beerCount > 0 && DrinkManagerListActivity.this.beerPrice != null && DrinkManagerListActivity.this.beerPrice.getText().toString() != "") {
						try {
							beerFinalPrice = DrinkManagerListActivity.this.beerCount * Float.parseFloat(DrinkManagerListActivity.this.beerPrice.getText().toString().replaceAll(",", "."));
						} catch (NumberFormatException e) {
							Toast toast = Toast.makeText(DrinkManagerListActivity.this.getApplicationContext(), getResources().getText(R.string.beerPricePrompt), Toast.LENGTH_SHORT);
							toast.show();
						}
					}
				}

				if (enableWine) {
					if (DrinkManagerListActivity.this.wineCount > 0 && DrinkManagerListActivity.this.winePrice != null && DrinkManagerListActivity.this.winePrice.getText().toString() != "") {
						try {
							wineFinalPrice = DrinkManagerListActivity.this.wineCount * Float.parseFloat(DrinkManagerListActivity.this.winePrice.getText().toString().replaceAll(",", "."));
						} catch (NumberFormatException e) {
							Toast toast = Toast.makeText(DrinkManagerListActivity.this.getApplicationContext(), getResources().getText(R.string.winePricePrompt), Toast.LENGTH_SHORT);
							toast.show();
						}
					}
				}

				if (enableShot) {
					if (DrinkManagerListActivity.this.shotCount > 0 && DrinkManagerListActivity.this.shotPrice != null && DrinkManagerListActivity.this.shotPrice.getText().toString() != "") {
						try {
							shotFinalPrice = DrinkManagerListActivity.this.shotCount * Float.parseFloat(DrinkManagerListActivity.this.shotPrice.getText().toString().replaceAll(",", "."));
						} catch (NumberFormatException e) {
							Toast toast = Toast.makeText(DrinkManagerListActivity.this.getApplicationContext(), getResources().getText(R.string.shotPricePrompt), Toast.LENGTH_SHORT);
							toast.show();
						}
					}
				}

				float finalPrice = beerFinalPrice + shotFinalPrice + wineFinalPrice;
				DrinkManagerListActivity.this.finalPriceText.setText(String.valueOf(finalPrice) + "");
				Log.d(TAG, "price = " + String.valueOf(finalPrice) + "");
				DrinkManagerListActivity.this.alcoholConcentrationText.setText(String.valueOf(this.alcoholConcentration * 1000) + " \u2030");
				Log.d(TAG, "alcoholConcentration = " + String.valueOf(this.alcoholConcentration * 1000) + " \u2030");

				DrinkManagerListActivity.this.drunkRating.setRating(this.drunkRating);
				// Toast.makeText(DrinkManagerActivity.this.getApplicationContext(),
				// "Alcohol concentration is: " +
				// String.valueOf(this.alcoholConcentration * 1000) + " ‰",
				// Toast.LENGTH_SHORT).show();
			}
		});
		this.mapButton = (ImageButton) this.findViewById(R.id.map);
		mapButton.setVisibility(View.VISIBLE);
		this.mapButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Toast.makeText(getApplicationContext(),
				// getResources().getText(R.string.alreadyToast),
				// Toast.LENGTH_SHORT).show();

				Intent intent = new Intent(getApplicationContext(), DrinkMapsActivity.class);
				startActivity(intent);
			}
		});
		this.placesButton = (ImageButton) this.findViewById(R.id.places);
		placesButton.setVisibility(View.VISIBLE);
		this.placesButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), DrinkPlacesActivity.class);
				startActivity(intent);
			}
		});

		this.settingsButton = (ImageButton) this.findViewById(R.id.settings);
		settingsButton.setVisibility(View.VISIBLE);
		this.settingsButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
				startActivity(intent);
			}
		});

		this.drinksButton = (ImageButton) this.findViewById(R.id.drinks);

		this.drinksButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Intent intent = new Intent(getApplicationContext(),
				// DrinkManagerActivity.class);
				// startActivity(intent);
				Toast.makeText(getApplicationContext(), getResources().getText(R.string.alreadyToast), Toast.LENGTH_SHORT).show();

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = this.getMenuInflater();
		inflater.inflate(R.menu.optionmenu, menu);
		MenuItem settings = menu.findItem(R.id.settings);
		settings.setIntent(new Intent(this, SettingsActivity.class));
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.exit:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(getResources().getText(R.string.exitPrompt)).setCancelable(true).setPositiveButton(getResources().getText(R.string.yesAnswer), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Toast.makeText(DrinkManagerListActivity.this.getApplicationContext(), getResources().getText(R.string.exitMessage), Toast.LENGTH_SHORT).show();
					DrinkManagerListActivity.this.finish();
				}
			}).setNegativeButton(getResources().getText(R.string.noAnswer), new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
			AlertDialog alert = builder.create();
			alert.show();
			// this.onDestroy();
			return true;
		case R.id.settings:
			Intent settingsIntent = item.getIntent();
			DrinkManagerListActivity.this.startActivityForResult(settingsIntent, 0);
			return true;
		case android.R.id.home:
			Toast.makeText(DrinkManagerListActivity.this.getApplicationContext(), getResources().getText(R.string.alreadyToast), Toast.LENGTH_SHORT).show();
			// Intent intent = new Intent(this, DrinkManagerListActivity.class);
			// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			// startActivity(intent);
			return true;
		case R.id.drinks:
			Toast.makeText(DrinkManagerListActivity.this.getApplicationContext(), getResources().getText(R.string.alreadyToast), Toast.LENGTH_SHORT).show();
			return true;
		case R.id.places:
			Intent placesIntent = new Intent(getApplicationContext(), DrinkPlacesActivity.class);
			this.startActivityForResult(placesIntent, 0);
			return true;
//		case R.id.graphs:
//			Intent graphsIntent = new Intent(getApplicationContext(), DrinkGraphActivity.class);
//			this.startActivityForResult(graphsIntent, 0);
//			return true;
		case R.id.back:
			Intent backIntent = new Intent(getApplicationContext(), BackgroundChooserActivity.class);
			this.startActivityForResult(backIntent, 0);
			return true;
		case R.id.map:
			Intent mapsIntent = new Intent(getApplicationContext(), DrinkMapsActivity.class);
			DrinkManagerListActivity.this.startActivityForResult(mapsIntent, 0);

			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "Resumed to drink manager screen");
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		BackgroundAdapter.updateBackgroundFromPrefs(prefs, this);
		this.drinks = new ArrayList<Drink>();
		// this.checkAndSetVisibility(prefs);
	}

	public void checkAndSetVisibility(SharedPreferences prefs) {
		boolean enableDrinks = prefs.getBoolean("EnableDrinks", false);
		boolean enableSoftDrinks = prefs.getBoolean("EnableSoftDrinks", false);
		boolean enableBeer = false;
		enableBeer = prefs.getBoolean("EnableBeer", true);
		boolean enableWine = false;
		enableWine = prefs.getBoolean("EnableWine", true);
		boolean enableShot = false;
		enableShot = prefs.getBoolean("EnableShot", true);

		if (!enableBeer) {
			this.beerButton.setVisibility(android.view.View.INVISIBLE);
			this.beerPrice.setVisibility(android.view.View.INVISIBLE);
			this.beerCountLabel.setVisibility(android.view.View.INVISIBLE);
			this.beerCountText.setVisibility(android.view.View.INVISIBLE);
			// this.beerBar.setVisibility(android.view.View.INVISIBLE);
			this.beerPriceCurrencyLabel.setVisibility(android.view.View.INVISIBLE);
			this.beerCount = 0;
			// this.beerBar.setProgress(0);
		} else {
			this.beerButton.setVisibility(android.view.View.VISIBLE);
			this.beerPrice.setVisibility(android.view.View.VISIBLE);
			this.beerCountLabel.setVisibility(android.view.View.VISIBLE);
			this.beerCountText.setVisibility(android.view.View.VISIBLE);
			// this.beerBar.setVisibility(android.view.View.VISIBLE);
			this.beerPriceCurrencyLabel.setVisibility(android.view.View.VISIBLE);
		}
		if (!enableWine) {
			this.wineButton.setVisibility(android.view.View.INVISIBLE);
			this.winePrice.setVisibility(android.view.View.INVISIBLE);
			this.wineCountLabel.setVisibility(android.view.View.INVISIBLE);
			this.wineCountText.setVisibility(android.view.View.INVISIBLE);
			// this.wineBar.setVisibility(android.view.View.INVISIBLE);
			this.winePriceCurrencyLabel.setVisibility(android.view.View.INVISIBLE);
			this.wineCount = 0;
			// this.wineBar.setProgress(0);
		} else {
			this.wineButton.setVisibility(android.view.View.VISIBLE);
			this.winePrice.setVisibility(android.view.View.VISIBLE);
			this.wineCountLabel.setVisibility(android.view.View.VISIBLE);
			this.wineCountText.setVisibility(android.view.View.VISIBLE);
			// this.wineBar.setVisibility(android.view.View.VISIBLE);
			this.winePriceCurrencyLabel.setVisibility(android.view.View.VISIBLE);
		}
		if (!enableShot) {
			this.shotButton.setVisibility(android.view.View.INVISIBLE);
			this.shotPrice.setVisibility(android.view.View.INVISIBLE);
			this.shotCountLabel.setVisibility(android.view.View.INVISIBLE);
			this.shotCountText.setVisibility(android.view.View.INVISIBLE);
			// this.shotBar.setVisibility(android.view.View.INVISIBLE);
			this.shotPriceCurrencyLabel.setVisibility(android.view.View.INVISIBLE);
			this.shotCount = 0;
			// this.shotBar.setProgress(0);
		} else {
			this.shotButton.setVisibility(android.view.View.VISIBLE);
			this.shotPrice.setVisibility(android.view.View.VISIBLE);
			this.shotCountLabel.setVisibility(android.view.View.VISIBLE);
			this.shotCountText.setVisibility(android.view.View.VISIBLE);
			// this.shotBar.setVisibility(android.view.View.VISIBLE);
			this.shotPriceCurrencyLabel.setVisibility(android.view.View.VISIBLE);
		}

		if (enableDrinks) {

		} else {

		}
		if (enableSoftDrinks) {

		} else {

		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "Paused drink manager screen");
	}

	@Override
	protected void onDestroy() {
		Log.d(TAG, "Destroyed drink manager screen");
		this.drinks = new ArrayList<Drink>();
		super.onDestroy();
	}
}